package com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.View;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Create.SelectPage.View.SelectPage;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.Store.Store;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.Store.ViewDatas.CommentListItem;
import com.huang825172.sbog.Util.VerticalViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoPage extends Fragment implements View.OnClickListener , IDataChangedListener, ViewPager.OnPageChangeListener {

    private static final String typeKey = "VideoPageType";
    private String videoPageType;

    private OnFragmentInteractionListener mListener;

    private Store store;
    private ActionCreator actionCreator;
    private VideoPageViewPagerAdapter adapter;
    private int currentPage = 1;
    private int currentPos = 0;
    private boolean like = false;

    @BindView(R.id.VideoPagePagesViewPager)
    VerticalViewPager verticalViewPager;

    @BindView(R.id.VideoPageCameraEntrenceImageView)
    ImageView cameraImageView;

    @BindView(R.id.VideoPageRootView)
    FrameLayout rootView;

    @BindView(R.id.VideoPageShareImageView)
    ImageView shareImageView;
    @BindView(R.id.VideoPageCommentsImageView)
    ImageView commentsImageView;

    @BindView(R.id.VideoPageShareFoldImageView)
    ImageView shareFoldImageView;
    @BindView(R.id.VideoPageShareBox)
    RelativeLayout shareBox;

    @BindView(R.id.VideoPageLikeImageView)
    ImageView likeImageView;

    @BindView(R.id.VideoPageCommentFoldImageView)
    ImageView commentFoldImageView;
    @BindView(R.id.VideoPageCommentContainer)
    RelativeLayout commentBox;

    @BindView(R.id.VideoPageCommentRecycleView)
    RecyclerView commentRecyclerView;

    public VideoPage() {
        // Required empty public constructor
    }

    public static VideoPage newInstance(String type) {
        VideoPage fragment = new VideoPage();
        Bundle args = new Bundle();
        args.putString(typeKey, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            videoPageType = getArguments().getString(typeKey);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_page, container, false);
        ButterKnife.bind(this, view);

        store = new Store(getContext());
        actionCreator = new ActionCreator();
        store.setDataChangedListener(this);
        Dispatcher.getSingleton().register(store);
        actionCreator.createGetVideoAction(currentPage);

        ArrayList<View> viewLists = new ArrayList<>();
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));
        viewLists.add(getLayoutInflater().inflate(R.layout.viewpager_video_page_view, null, false));//翻页用
        adapter = new VideoPageViewPagerAdapter(viewLists, store.getVideoData());
        adapter.setType(videoPageType);
        verticalViewPager.setAdapter(adapter);
        verticalViewPager.setCurrentItem(0);
        verticalViewPager.setOnPageChangeListener(this);

        cameraImageView.setOnClickListener(this);
        shareImageView.setOnClickListener(this);
        likeImageView.setOnClickListener(this);
        commentsImageView.setOnClickListener(this);

        commentBox.setVisibility(View.INVISIBLE);

        List<CommentListItem> Data = new ArrayList<>();
        Data.add(new CommentListItem("", "15915587585", "获取评论的代码和信息在一起", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "但是发评论的接口有点问题", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "所以没有嗯", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "IM功能需要氪金哎", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "在这交代码之前的晚上", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "还有马克思的网课和Pre", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "交了代码我要摸会鱼", "1-1", true, 100));
        Data.add(new CommentListItem("", "15915587585", "233", "1-1", true, 100));
        commentRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        commentRecyclerView.setAdapter(new VideoPageCommentListRecyclerViewAdapter(Data));

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.VideoPageCameraEntrenceImageView: {
                Intent intent = new Intent();
                intent.setClass(getContext(), SelectPage.class);
                startActivity(intent);
                break;
            }
            case R.id.VideoPageShareImageView:{
                ObjectAnimator.ofFloat(shareBox,"translationY",0F,-360F)
                        .setDuration(300).start();
                shareFoldImageView.setOnClickListener(this);
                shareImageView.setOnClickListener(null);
                break;
            }
            case R.id.VideoPageCommentsImageView: {
                commentBox.setVisibility(View.VISIBLE);
                commentFoldImageView.setOnClickListener(this);
                commentsImageView.setOnClickListener(null);
                break;
            }
            case R.id.VideoPageShareFoldImageView:{
                ObjectAnimator.ofFloat(shareBox,"translationY",-360F,0F)
                        .setDuration(300).start();
                shareFoldImageView.setOnClickListener(null);
                shareImageView.setOnClickListener(this);
                break;
            }
            case R.id.VideoPageCommentFoldImageView: {
                commentBox.setVisibility(View.INVISIBLE);
                commentFoldImageView.setOnClickListener(null);
                commentsImageView.setOnClickListener(this);
                break;
            }
            case R.id.VideoPageLikeImageView:{
                like = !like;
                // 假装换个图标2333有够摸鱼
                // 算了还是写点逻辑代码
                // 此处请求之后再发个 DataChange 回来校验就是了
                // 以下代码应该写在 onDataChange 里面的
                if (store.getCurrentVideoInfo() != null) {
                    actionCreator.createLikeAction(store.getCurrentVideoInfo().data.id);
                }
                if(like){
                    likeImageView.setImageResource(R.mipmap.video_like);
                }else {
                    likeImageView.setImageResource(R.mipmap.video_unlike);
                }
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label){
            case "VideoPageGetListSuccess":{
                //Snackbar.make(rootView, "Get Video Success", Snackbar.LENGTH_SHORT).show();
                adapter.UpdateData(store.getVideoData());
                adapter.notifyDataSetChanged();
                verticalViewPager.setCurrentItem(0);
                break;
            }
            case "VideoPageGetListFail":{
                //Snackbar.make(rootView, "Get Video Fail", Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "VideoPageGetInfoSuccess":{
                //Snackbar.make(rootView, "Get Video Info Success", Snackbar.LENGTH_SHORT).show();
                adapter.UpdataInfo(store.getCurrentVideoInfo());
                adapter.notifyDataSetChanged();
                verticalViewPager.setCurrentItem(currentPos);
                break;
            }
            case "VideoPageGetInfoFail":{
                //Snackbar.make(rootView, "Get Video Info Fail", Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "VideoPageOpenShopAction":{
                Uri uri = Uri.parse("https://detail.tmall.com/item.htm?spm=a230r.1.14.133.300517c5bPuAsK&id=569573974969&ns=1&abbucket=14");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
            default:
                //Snackbar.make(rootView, label, Snackbar.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onPageSelected(int i) {
        currentPos = i;

        if(store.getVideoData().size() >= i+1){
            int id = store.getVideoData().get(i).id;
            actionCreator.createGetVideoInfoAction(id);
        }

        if(i == 10){
            actionCreator.createGetVideoAction(++currentPage);
            Snackbar.make(rootView,"正在加载下一页",Snackbar.LENGTH_SHORT).show();
        }
    }
}
